/***********
 * Socket接口
 */
package com.yy.ttmodule.cardStatus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.WorkSource;
import android.util.Log;

public class SocketInterface {

    private static final String TAG = "SocketInterface";
    
    public static final int MSG_ID_SEND_COMMAND = 0x01;
    public static final int MSG_ID_REMOVE_WORKER = 0x02;
    private static final String SOCKET_NAME = "com.dtt.bmserver";
    private LocalServerSocket mServerSocket;
    private List<BmWorker> mWorkers = new ArrayList<BmWorker>();
    private int id = 0;
    private HandlerThread mHandlerThread;
    private CommandDispatchHandler mHandler;
    private BmService mService;
    public SocketInterface(BmService service) {
        this.mService = service;
	    //新建命令分发线程
        mHandlerThread = new HandlerThread("CommandDistribute");
        mHandlerThread.start();
        mHandler = new CommandDispatchHandler(mHandlerThread.getLooper());
        try {
            mServerSocket = new LocalServerSocket(SOCKET_NAME);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        new AcceptThread("AcceptThread").start();//接收命令线程
    }
    
    private class AcceptThread extends Thread {
        boolean can_run = true;
        
        public AcceptThread(String name) {
            super(name);
        }
        
        @Override
        public void run() {
            while (can_run) {
                LocalSocket mSocket = null;
                if (mServerSocket == null) {
                    return;
                }                
                try {//一个工作者连接到服务器
                    //鎺ュ彈杩炴帴璇锋眰锛屽苟鑾峰彇soket瀵硅薄
                    mSocket = mServerSocket.accept();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    mSocket = null;
                }
                if (mSocket != null) {
                    mWorkers.add(new BmWorker(mService, mSocket, id, mHandler));
                    id++;
                }
            }
        }
    }
    
    public class CommandDispatchHandler extends Handler {

        public CommandDispatchHandler(Looper looper)
        {
            super(looper);
        }
        
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
        	switch (msg.what) {
                case MSG_ID_SEND_COMMAND://
                    Command command = (Command) msg.obj;
                    sendCommandToModem(command);
                    break;
                case MSG_ID_REMOVE_WORKER://
                    BmWorker worker = (BmWorker) msg.obj;
                    if (mWorkers.contains(worker))
                    {
                        mWorkers.remove(worker);
                        Log.d(TAG,"mWorkers size = " + mWorkers.size());
                    }
                    break;
            }
        }
    }
    
    /**
     * 鍙戦�佸懡浠ょ粰modem
     * @param command
     *///发送命令到Modem
    public void sendCommandToModem(Command command) {
        if ((mService == null) || (command == null)) {
            Log.d(TAG, "mService = " + mService + "command = " + command);
            return;
        }
        mService.getModemInterface().addCommandToWriteList(command);
    }
    
    /**
     * 閫氳繃workerid 鎵惧埌瀵瑰簲鐨剋orker 骞跺彂閫佸搷搴旀秷鎭�
     * @param command
     */
    public void sendResponse(Command command) {
        //命令响应,发送到指定工作者
        int id = command.getWorkerId();
        for (BmWorker worker : mWorkers) {
            if (worker.workerId == id) {
                worker.sendResponse(command);
            }
        }
    }

}
